unit unEventos;

interface

uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, Grids, DBGrids, ThemeDBGrid, DB, StdCtrls, Buttons, ComCtrls,
 Pergunta, DBCtrls;

type
 TfmEventos = class(TForm)
  btResultados: TBitBtn;
  btFechar: TBitBtn;
  PageControl1: TPageControl;
  TabSheet1: TTabSheet;
  ThemeDBGrid1: TThemeDBGrid;
  btSalvar: TBitBtn;
  PageControl2: TPageControl;
  TabSheet2: TTabSheet;
  Label1: TLabel;
  Label2: TLabel;
  Label3: TLabel;
  Label4: TLabel;
  Label5: TLabel;
  Label6: TLabel;
  Label7: TLabel;
  Label9: TLabel;
  Label10: TLabel;
  Label11: TLabel;
  Label12: TLabel;
  Label13: TLabel;
  Label14: TLabel;
  Label15: TLabel;
  Label8: TLabel;
  Label16: TLabel;
  Label17: TLabel;
  Label18: TLabel;
  Label19: TLabel;
  Label20: TLabel;
  Label21: TLabel;
  Label22: TLabel;
  Label23: TLabel;
  Label24: TLabel;
  Label25: TLabel;
  Label26: TLabel;
  Label27: TLabel;
  Label28: TLabel;
  Label29: TLabel;
  Label30: TLabel;
  Label31: TLabel;
  Label32: TLabel;
  DBLookupComboBox1: TDBLookupComboBox;
  DBLookupComboBox2: TDBLookupComboBox;
  DBLookupComboBox3: TDBLookupComboBox;
  DBLookupComboBox4: TDBLookupComboBox;
  DBLookupComboBox5: TDBLookupComboBox;
  DBLookupComboBox6: TDBLookupComboBox;
  DBLookupComboBox7: TDBLookupComboBox;
  DBLookupComboBox8: TDBLookupComboBox;
  DBLookupComboBox9: TDBLookupComboBox;
  DBLookupComboBox10: TDBLookupComboBox;
  DBLookupComboBox11: TDBLookupComboBox;
  DBLookupComboBox12: TDBLookupComboBox;
  DBLookupComboBox13: TDBLookupComboBox;
  DBLookupComboBox14: TDBLookupComboBox;
  DBLookupComboBox15: TDBLookupComboBox;
  DBLookupComboBox16: TDBLookupComboBox;
  btCancelarEvento: TBitBtn;
  dsEvento: TDataSource;
  dsPilotos: TDataSource;
  Pergunta1: TPergunta;
  ThemeDBGrid2: TThemeDBGrid;
  dsPenalizacoes: TDataSource;
  BitBtn1: TBitBtn;
  BitBtn2: TBitBtn;
  Pergunta2: TPergunta;
  procedure btFecharClick(Sender: TObject);
  procedure FormCreate(Sender: TObject);
  procedure FormClose(Sender: TObject; var Action: TCloseAction);
  procedure btResultadosClick(Sender: TObject);
  procedure FormKeyPress(Sender: TObject; var Key: Char);
  procedure ThemeDBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
   DataCol: Integer; Column: TColumn; State: TGridDrawState);
  procedure btSalvarClick(Sender: TObject);
  procedure btCancelarEventoClick(Sender: TObject);
  procedure FormShow(Sender: TObject);
  procedure dsEventoDataChange(Sender: TObject; Field: TField);
  procedure dsEventoStateChange(Sender: TObject);
  procedure BitBtn1Click(Sender: TObject);
  procedure BitBtn2Click(Sender: TObject);
 private
  { Private declarations }
 public
  { Public declarations }
 end;
 
var
 fmEventos: TfmEventos;
 
implementation

uses unDM, unResultados, unGrafico, unPenalidade;

{$R *.dfm}

procedure TfmEventos.BitBtn1Click(Sender: TObject);
begin
 fmPenalidade := TfmPenalidade.Create(Self);
 dm.tabPenalizacoes.Append;
 fmPenalidade.ShowModal;
 fmPenalidade.Free;
 btSalvar.Enabled := True;
 dm.tabEventos.Edit;
end;

procedure TfmEventos.BitBtn2Click(Sender: TObject);
begin
 if dm.tabPenalizacoes.RecordCount > 0 then
  begin
   dm.tabPenalizacoes.Delete;
   btSalvar.Enabled := True;
   dm.tabEventos.Edit;
  end;
end;

procedure TfmEventos.btCancelarEventoClick(Sender: TObject);
begin
 if Pergunta1.Executar = mrYes then
  begin
   dm.tabEventos.Edit;
   dm.tabEventosPrimeiro.Value := -1;
   dm.tabEventosSegundo.Value := -1;
   dm.tabEventosTerceiro.Value := -1;
   dm.tabEventosQuarto.Value := -1;
   dm.tabEventosQuinto.Value := -1;
   dm.tabEventosSexto.Value := -1;
   dm.tabEventosSetimo.Value := -1;
   dm.tabEventosOitavo.Value := -1;
   dm.tabEventosNono.Value := -1;
   dm.tabEventosDecimo.Value := -1;
   dm.tabEventosDecimoPrimeiro.Value := -1;
   dm.tabEventosDecimoSegundo.Value := -1;
   dm.tabEventosDecimoTerceiro.Value := -1;
   dm.tabEventosDecimoQuarto.Value := -1;
   dm.tabEventosDecimoQuinto.Value := -1;
   dm.tabEventosDecimoSexto.Value := -1;
   dm.tabEventosFinalizada.Value := False;
   dm.tabEventos.Post;

   dm.sqlTemp.Close;
   dm.sqlTemp.SQL.Clear;
   dm.sqlTemp.SQL.Text := 'DELETE FROM penalizacoes WHERE idevento=:IDEVENTO';
   dm.sqlTemp.Params[0].AsInteger := dm.tabEventosidevento.Value;
   dm.sqlTemp.ExecuteSQL;
   dm.tabPenalizacoes.Refresh;

   Application.ProcessMessages;
   dm.AtualizarPontos();
  end;
end;

procedure TfmEventos.btFecharClick(Sender: TObject);
begin
 if dm.tabEventos.State = dsEdit then
  btSalvar.Click;
 
 Close;
end;

procedure TfmEventos.btResultadosClick(Sender: TObject);
begin
 // fmResultados := TfmResultados.Create(Self);
 // dm.tabEventos.Edit;
 // fmResultados.TabSheet1.Caption := dm.tabEventosEvento.Value;
 // fmResultados.ShowModal;
 // fmResultados.Free;
end;

procedure TfmEventos.btSalvarClick(Sender: TObject);
begin
 dm.tabEventosFinalizada.Value := True;
 dm.tabEventos.Post;
 btSalvar.Enabled := False;
 Application.ProcessMessages;
 dm.AtualizarPontos();
end;

procedure TfmEventos.dsEventoDataChange(Sender: TObject; Field: TField);
begin
 TabSheet2.Caption := dm.tabEventosEvento.Value;
 dm.tabPenalizacoes.Close;
 dm.tabPenalizacoes.Params[0].AsInteger := dm.tabEventosidevento.Value;
 dm.tabPenalizacoes.Open;
 
 btCancelarEvento.Enabled := dm.tabEventosFinalizada.Value;
 
end;

procedure TfmEventos.dsEventoStateChange(Sender: TObject);
begin
 if DM.tabEventos.State = dsEdit then
  btSalvar.Enabled := True;
end;

procedure TfmEventos.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 dm.tabEventos.Close;
 DM.tabPilotos.Close;
 DM.tabPenalizacoes.Close;
end;

procedure TfmEventos.FormCreate(Sender: TObject);
begin
 dm.tabEventos.Close;
 dm.tabEventos.SQL.Clear;
 dm.tabEventos.SQL.Text := 'SELECT * FROM eventos ORDER BY idevento';
 dm.tabEventos.Open;
 DM.tabPilotos.Open;
 
 dm.tabPenalizacoes.Close;
 dm.tabPenalizacoes.Params[0].AsInteger := dm.tabEventosidevento.Value;
 dm.tabPenalizacoes.Open;
end;

procedure TfmEventos.FormKeyPress(Sender: TObject; var Key: Char);
begin
 if key = #27 then
  Close;
end;

procedure TfmEventos.FormShow(Sender: TObject);
begin
 ThemeDBGrid1.SetFocus;
 // ThemeDBGrid1CellClick(ThemeDBGrid1.Columns[0]);
end;

procedure TfmEventos.ThemeDBGrid1DrawColumnCell(Sender: TObject;
 const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
 if dm.tabEventosFinalizada.Value = True then
  if ThemeDBGrid1.Canvas.Brush.Color = ThemeDBGrid1.Color then
   ThemeDBGrid1.Canvas.Brush.Color := clMoneyGreen;
 
 ThemeDBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

end.

